package com.imooc.ecommerce.transactional;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.IOException;

/**
 * <p>
 *
 * @author Yaxi.Zhang
 * @since 2022/11/16 17:32
 */
@Slf4j
@Service
public class AnotherTransactional {
    /**
     * springboot_user dao
     */
    private final SpringBootUserRepository springBootUserRepository;

    public AnotherTransactional(SpringBootUserRepository springBootUserRepository) {
        this.springBootUserRepository = springBootUserRepository;
    }

    @Transactional(rollbackFor = Exception.class)
    public void rightCall() throws Exception {
        JpaSpringBootUser springBootUser = new JpaSpringBootUser();
        springBootUser.setUsername("ImoocQinyi-2021-01-01");
        springBootUserRepository.save(springBootUser);

        // ....
        // 由于某种原因抛出了异常
        throw new IOException("throw io exception for check rollback");
    }

}
